//package com.ghs.securityprojectboot.filters;
//
//import com.alibaba.fastjson.JSONObject;
//import com.ghs.securityprojectboot.entity.vo.RestBean;
//import com.ghs.securityprojectboot.util.JWTUtil;
//import jakarta.servlet.FilterChain;
//import jakarta.servlet.ServletException;
//import jakarta.servlet.http.HttpServletRequest;
//import jakarta.servlet.http.HttpServletResponse;
//import org.springframework.stereotype.Component;
//import org.springframework.web.filter.OncePerRequestFilter;
//
//import java.io.IOException;
//import java.util.Map;
//
//@Component
//public class AuthenticationTokenFilter extends OncePerRequestFilter {
//
//    @Override
//    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
//        response.setContentType("application/json;charset=utf-8");
//        if (request.getRequestURI().startsWith("/api/auth/")&& "POST".equals(request.getMethod())) {
//            filterChain.doFilter(request, response);
//            return;
//        }
//        String token = request.getHeader("token");
//        if (token == null || !JWTUtil.verify(token)) {
//            writeErrorResponse(response, "用户未登录或token失效，请重新登录！");
//            return;
//        }
//
//        Map<String, Object> userinfo = JWTUtil.getInfo(token, "userinfo");
//        if (userinfo == null || !userinfo.containsKey("username") || !userinfo.containsKey("permissions")) {
//            writeErrorResponse(response, "用户信息获取失败！");
//            return;
//        }
//        filterChain.doFilter(request, response);
//    }
//
//    private void writeErrorResponse(HttpServletResponse response, String message) throws IOException {
//        response.getWriter().write(JSONObject.toJSONString(RestBean.failure(403, message)));
//    }
//}